package com.txunda.zhuohired.adapter.user.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/28 13:56
 * 功能描述：智能排序adapter
 * 联系方式：jiahuanchen@126.com
 */

public class CapacityAdapter extends BaseAdapter {
    private Context context;
    private List<String> capacityList;
    private int capacity;
    private ViewHolder holder;
    /**
     * 自定义监听
     */
    private HolderClickListener mHolderClickListener;

    public CapacityAdapter(Context context, List<String> capacityList, int capacity) {
        this.context = context;
        this.capacityList = capacityList;
        this.capacity = capacity;
    }

    @Override
    public int getCount() {
        return capacityList.size();
    }

    @Override
    public Object getItem(int i) {
        return capacityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_capacity, null);
            holder = new ViewHolder();
            view.setTag(holder);
            ViewUtils.inject(holder, view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (i == capacity) {
            //选中
            holder.tv_item_capacity.setTextColor(context.getResources().getColor(R.color.blue));
            holder.iv_item_capacity.setVisibility(View.VISIBLE);
        } else {
            //未选中
            holder.tv_item_capacity.setTextColor(context.getResources().getColor(R.color.home_grey3));
            holder.iv_item_capacity.setVisibility(View.GONE);
        }
        holder.tv_item_capacity.setText(capacityList.get(i).toString());
        holder.ll_item_capacity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                capacity = i;
                notifyDataSetChanged();
                mHolderClickListener.onHolderClick(i);
            }
        });
        return view;
    }

    class ViewHolder {
        /**
         * 整个条目
         */
        @ViewInject(R.id.ll_item_capacity)
        private LinearLayout ll_item_capacity;
        /**
         * 内容
         */
        @ViewInject(R.id.tv_item_capacity)
        private TextView tv_item_capacity;
        /**
         * 勾选
         */
        @ViewInject(R.id.iv_item_capacity)
        private ImageView iv_item_capacity;

    }

    /**
     * 自定义监听
     *
     * @param holderClickListener
     */
    public void SetClickListener(HolderClickListener holderClickListener) {
        this.mHolderClickListener = holderClickListener;
    }

    /**
     * 自定义监听
     *
     * @
     */
    public interface HolderClickListener {
        void onHolderClick(int position);
    }
}
